package com.ruoyi.web.controller.storage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.storage.domain.RepoCk;
import com.ruoyi.storage.domain.RepoRk;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoCkService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/storage/ck")
public class CKManageController extends BaseController {
    @Autowired
    private RepoCkService repoCkService;

    private String prefix = "/storage/ck";

    @RequiresPermissions("storage:ck:view")
    @RequestMapping("")
    public String index(){
        return prefix + "/ck_index";
    }

    /**
     * 查看出库列表
     * @return
     */
    @RequiresPermissions("storage:ck:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepoCk repoCk)
    {
        startPage();
        List<RepoCk> list = repoCkService.selectAllCkList(repoCk);
        return getDataTable(list);
    }


    /**
     * 展示信息
     */
    @RequiresPermissions("storage:ck:detail")
    @GetMapping("/detail/{bid}")
    public String view(@PathVariable("bid") Integer id, ModelMap mmap)
    {
        RepoCk repoCk = new RepoCk();
        repoCk.setCkId(id);
        List<RepoCk> list = repoCkService.selectAllCkList(repoCk);

        mmap.put("repoCk", list.get(0));
        return prefix + "/view";
    }


    /**
     * 携带参数到修改的判断界面
     */
//    @RequiresPermissions("storage:ck:edit")
////    @GetMapping("/edit/{bid}/{status}")
//    @GetMapping("/edit/{status}/{id}")
//    public String edit(@PathVariable("id") Integer id,@PathVariable("status") Integer status,ModelMap mmap)
//    {
//        mmap.addAttribute("ckId",id);
//        mmap.addAttribute("status",status.toString());
//        return prefix + "editStatus";
//    }

    /**
     * 修改信息
     */
    @RequiresPermissions("storage:rk:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(RepoCk repoCk, ModelMap mmap)
    {
        if ("2".equals(repoCk.getStatus())) {
            System.out.println("===========================================================================");
            RepoCk repoCk1 = new RepoCk();
            repoCk1.setCkId(repoCk.getCkId());
            List<RepoCk> repoCks = repoCkService.selectAllCkList(repoCk1);
            if (repoCks.size() != 0){
                Integer wareId = repoCkService.getWareIdByCkId(repoCks.get(0));
                HashMap<String,Object> map = repoCkService.getProductIdAndNumByCkId(repoCks.get(0));
                Integer productId = (Integer) map.get("productId");
                Integer num = (Integer)map.get("num");
                boolean b = repoCkService.updateKcReduce(wareId,productId,num);
            }

        }
        if ("5".equals(repoCk.getStatus())) {
            System.out.println("===========================================================================");
            RepoCk repoCk1 = new RepoCk();
            repoCk1.setCkId(repoCk.getCkId());
            List<RepoCk> repoCks = repoCkService.selectAllCkList(repoCk1);
            if (repoCks.size() != 0){
                Integer wareId = repoCkService.getWareIdByCkId(repoCks.get(0));
                HashMap<String,Object> map = repoCkService.getProductIdAndNumByCkId(repoCks.get(0));
                Integer productId = (Integer) map.get("productId");
                Integer num = (Integer)map.get("num");
                boolean b = repoCkService.updateKcAdd(wareId,productId,num);
            }

        }
        return toAjax(repoCkService.updateCkStatus(repoCk));
    }


    /**
     * 创建仓库是实现跳转
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<String> list = repoCkService.getAllMarketIndents();
        List<RepoWare> repoWares = repoCkService.getAllWare();
        SysUser sysUser = getSysUser();
        model.addAttribute("list",list);
        model.addAttribute("repoWares",repoWares);
        model.addAttribute("sysUser",sysUser);
        return prefix + "/add";
    }

    /**
     * 新增保存仓库信息
     */
    @RequiresPermissions("storage:ck:add")
    @Log(title = "出库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RepoCk repoCk)
    {
        repoCk.setCkTime(new Date());
        repoCk.setSysCkUserId(getUserId().intValue());
        repoCk.setStatus("1");
        return toAjax(repoCkService.save(repoCk));
    }


}
